﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1.ViewModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            mvm = new MyViewModel(rows, cols);
            this.DataContext = mvm;



            GetValue();
        }

        private void GetValue()
        {
            // set rows and columns
            for (int i = 0; i < rows; i++)
            {
                var rowDef = new RowDefinition { Height = new GridLength(1, GridUnitType.Star) };
                BoxGrid.RowDefinitions.Add(rowDef);
            }

            for (int j = 0; j < cols; j++)
            {
                var colDef = new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) };
                BoxGrid.ColumnDefinitions.Add(colDef);
            }

            // fill rows and columns
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    var textBlock = new TextBlock { TextAlignment = TextAlignment.Center };
                    var bvm = new BoxViewModel { };

                    var binding = new Binding { Path = new PropertyPath("Text"), Source = bvm };
                    BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, binding);

                    BoxGrid.Children.Add(textBlock);
                    Grid.SetRow(textBlock, i);
                    Grid.SetColumn(textBlock, j);

                    mvm.BoxViewModels[i, j] = bvm;
                }
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            mvm.Switch();
        }

        private MyViewModel mvm; 
        private const int rows = 2;
        private const int cols = 3;
    }
}
